package com.china08.yxyapi.rabbitmq.listener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.config.RabbitConfig;
import com.china08.yxyapi.service.jpush.impl.JpushServiceImpl;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
@EnableRabbit
public class JpushForNotificationListener {

    @Autowired
    JpushServiceImpl jpushService;

    private static ObjectMapper objectMapper = new ObjectMapper();

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener1(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener2(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener3(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener4(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener5(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener6(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener7(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener8(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener9(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_NOTIFICATION)
    public void listener10(Map<String, String> bodyMap) throws JsonParseException, JsonMappingException, IOException {
        sendNotificationReady(bodyMap);
    }

    /**
     * 发送数据前准备
     * 
     * @param bodyMap
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @SuppressWarnings("unchecked")
    private void sendNotificationReady(Map<String, String> bodyMap)
            throws JsonParseException, JsonMappingException, IOException {
        String msgReceivers = bodyMap.get("msgReceivers").toString();
        String msgAlert = bodyMap.get("ALERT").toString();
        String msgTitle = bodyMap.get("TITLE").toString();
        Map<String, String> msgExtra = objectMapper.readValue(bodyMap.get("EXTRA"), Map.class);
        // String msgTag_and = bodyMap.get("msgTag_and").toString();
        String msgTag_and = bodyMap.get("msgTag_and");
        String date = bodyMap.get("date");

        // 电子书包来源
        String flag = bodyMap.get("flag");

        // 内容
        if (StringUtils.isBlank(msgAlert)) { return; }

        // 接收者集合
        String[] msgReceiversArr = msgReceivers.split(",");
        List<String> msgReceiversLst = Arrays.asList(msgReceiversArr);

        String[] msgTag_andArr = null;
        if (msgTag_and != null) {
            msgTag_andArr = msgTag_and.split(",");
        }

        String[] tags = new String[0];

        // 分组发送
        List<String> msgReceiversLstTmp = new ArrayList<>();

        if (StringUtils.isEmpty(date)) {
            for (String msgReceiver : msgReceiversLst) {
                // 临时集合
                msgReceiversLstTmp.add(msgReceiver);
                // 20
                if (msgReceiversLstTmp.size() == 20) {
                    // 发送
                    tags = msgReceiversLstTmp.toArray(new String[0]);
                    jpushService.sendNotificationPushToTags(msgAlert, msgTitle, msgExtra, tags, msgTag_andArr);
                    // 重置
                    msgReceiversLstTmp = new ArrayList<>();
                    tags = new String[0];
                }
            }
            // 不够20条发送一次
            tags = msgReceiversLstTmp.toArray(new String[0]);
            jpushService.sendNotificationPushToTags(msgAlert, msgTitle, msgExtra, tags, msgTag_andArr);
        } else {
            // 定时推送
            for (String msgReceiver : msgReceiversLst) {
                // 临时集合
                msgReceiversLstTmp.add(msgReceiver);
                // 20
                if (msgReceiversLstTmp.size() == 20) {
                    // 发送
                    tags = msgReceiversLstTmp.toArray(new String[0]);
                    jpushService.sendNotificationPushToTagsTiming(msgAlert, msgTitle, msgExtra, tags, msgTag_andArr,
                            date, flag);
                    // 重置
                    msgReceiversLstTmp = new ArrayList<>();
                    tags = new String[0];
                }
            }
            // 不够20条发送一次
            tags = msgReceiversLstTmp.toArray(new String[0]);
            jpushService.sendNotificationPushToTagsTiming(msgAlert, msgTitle, msgExtra, tags, msgTag_andArr, date,
                    flag);
        }

    }
}
